package com.peking.donations.base.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * User: zhujp Date: 2016/3/2 16:24 序列号生成工具类 Version: 1.0 To change this
 * templates use File | Settings | File Templates.
 */
public class SequenceUtil {

    /**
     * 生成订单号：D+12时间戳+3位随机数
     * 
     * @return
     */
    public static String getTransNoSys() {
        String transNoSys = null;
        String temp = "2016-01-01 00:00:00";
        try {
            // 12位时间戳
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date dateTemp = sdf.parse(temp);
            String subTime = String.format("%012d", (System.currentTimeMillis() - dateTemp.getTime()));
            // 3位随机数
            Random random = new Random();
            String randomNum = String.format("%03d", random.nextInt(1000));
            // 订单号
            transNoSys = String.format("%s%s%s", "D", subTime, randomNum);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return transNoSys;
    }

    /**
     * 获取一定长度的随机字符串
     * 
     * @param length
     *            指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    /**
     * 生成随机数
     * 
     * @param min
     * @param max
     * @return
     */
    public static int random(int min, int max) {
        Random r = new Random();
        int i = r.nextInt(max - min) + min;
        return i;
    }

}
